<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input v-model="crud.queryInput.value" clearable placeholder="输入搜索内容" style="width: 200px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <el-select v-model="crud.queryInput.type" clearable placeholder="类型" class="filter-item" style="width: 130px">
          <el-option v-for="item in queryTypeOptions" :key="item.key" :label="item.display_name" :value="item.key" />
        </el-select>
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="人员" prop="empId">
            <person-select ref="personSelect1" :value="[form.employeeBase.centerBase.departmentId.id,form.employeeBase.centerBase.id,form.empId]" @input="personSelectInput" />
          </el-form-item>
          <!-- <el-form-item label="人员">
            <el-input readonly="true" v-model="form.employeeBase.empName" style="width: 370px;" />
          </el-form-item> -->
          <el-form-item label="地点" prop="businessAdd">
            <el-input v-model="form.businessAdd" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="开始时间" prop="startTime">
            <el-date-picker v-model="form.startTime" type="datetime" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="结束时间">
            <el-date-picker v-model="form.endTime" type="datetime" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="事由">
            <el-input v-model="form.reason" :rows="3" type="textarea" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="类型">
            <!-- 手动设置 radio -->
            <el-radio-group v-model="form.type" style="width: 178px">
              <el-radio label="出差">出差</el-radio>
              <el-radio label="休假">休假</el-radio>
              <el-radio label="病假">病假</el-radio>
              <el-radio label="事假">事假</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column v-if="columns.visible('id')" prop="id" label="ID" />
        <!-- <el-table-column v-if="columns.visible('empId')" prop="empId.empName" label="人员id" /> -->

        <el-table-column v-if="columns.visible('empId')" prop="employeeBase.empName" label="人员">
          <template slot-scope="scope">
            <router-link :to="{ path: '/hr/hr/employee_base_one', query: { id: scope.row.empId }}">
              <el-tag>
                {{ scope.row.employeeBase.empName }}
              </el-tag>
            </router-link>
          </template>
        </el-table-column>
        <el-table-column v-if="columns.visible('businessAdd')" prop="businessAdd" label="地点" />
        <el-table-column v-if="columns.visible('startTime')" prop="startTime" label="开始时间">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.startTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column v-if="columns.visible('endTime')" prop="endTime" label="结束时间">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.endTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column v-if="columns.visible('reason')" prop="reason" label="事由" />
        <el-table-column v-if="columns.visible('type')" prop="type" label="类型" />
        <el-table-column v-permission="['admin','employeeState:edit','employeeState:del']" fixed="right" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudEmployeeState from '@/api/employeeState'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import { initData } from '@/api/data'
import personSelect from '@/views/components/cascader/person_cascader'

// crud交由presenter持有
const defaultCrud = CRUD({ title: '人员动态', url: 'api/employeeState', sort: 'id,asc', crudMethod: { ...crudEmployeeState }})
// const defaultForm = { id: null, empId: { id: null }, businessAdd: null, startTime: null, endTime: null, reason: null, type: null }
const defaultForm = {
  id: null,
  // empId: { id: null },
  empId: null,
  businessAdd: null,
  startTime: null,
  endTime: null,
  reason: null,
  type: null,
  employeeBase: { centerBase: { id: null, departmentId: { id: null }}}}

export default {
  name: 'EmployeeState',
  components: { pagination, crudOperation, rrOperation, udOperation, personSelect },
  mixins: [presenter(defaultCrud), header(), form(defaultForm), crud()],
  data() {
    return {
      permission: {
        add: ['admin', 'employeeState:add'],
        edit: ['admin', 'employeeState:edit'],
        del: ['admin', 'employeeState:del']
      },
      rules: {
        empId: [
          { required: true, message: '必须填写该项', trigger: 'blur' }
        ],
        businessAdd: [
          { required: true, message: '必须填写该项', trigger: 'blur' }
        ],
        startTime: [
          { required: true, message: '必须填写该项', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'id', display_name: 'ID' },
        // { key: 'empId', display_name: '人员' },
        { key: 'businessAdd', display_name: '地点' },
        { key: 'reason', display_name: '事由' },
        { key: 'type', display_name: '类型' }
      ],
      empAll: []
    }
  },
  created() {
    this.getEmpAll()
  },
  methods: {
    // 获取数据前设置好接口地址
    [CRUD.HOOK.beforeRefresh]() {
      // const query = this.query
      // if (query.type && query.value) {
      //   this.crud.params[query.type] = query.value
      // }
      // 叠加queryInput中的搜索条件
      if (this.crud.queryInput.type && this.crud.queryInput.value) {
        this.crud.query[this.queryInput.type] = this.queryInput.value
      }
      return true
    },
    personSelectInput(e) {
      // console.log(this.form)
      this.form.empId = e[2]
      this.form.employeeBase.centerBase.id = e[1]
      this.form.employeeBase.centerBase.departmentId.id = e[0]
    },
    getEmpAll() {
      // todo 下拉菜单考虑在后台返回一个简单对象
      return new Promise((resolve, reject) => {
        // 请求数据
        initData('api/employeeBase',
          { page: -1, size: 99999, sort: 'id,desc' }
        ).then(data => {
          // 获得数据
          this.empAll = data.content
          resolve(data)
        }).catch(err => {
          reject(err)
        })
      })
    },
    beforeCrudSubmitCU() {
      // 提交前处理this.crud.form数据
      const f = JSON.parse(JSON.stringify(this.form))
      f.employeeBase = null
      this.crud.form = f
    },
    afterCrudSubmitCU() {
      // 提交后还原
      this.crud.form = this.form
    }
  }
}
</script>

<style scoped>

</style>
